package com.yu.service.Impl;

import com.yu.dao.OrderDao;
import com.yu.domain.Order;
import com.yu.service.AccountService;
import com.yu.service.OrderService;
import com.yu.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author yutao
 * @date 2021/11/28 16:49
 */
@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    // 业务逻辑
    @Override
    //全局事务名称 name随便起，出现异常就回滚
    @GlobalTransactional(name = "fsp-create-order",rollbackFor = Exception.class)
    public void createOrder(Order order) {
        log.info("-------开始新建订单");
        orderDao.createOrder(order);
        log.info("----订单微服务开始调用库存，开始扣减");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("----库存扣减结束...");

        log.info("-----开始扣减金额");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("金额扣减结束");

        //修改订单状态
        log.info("-----修改订单状态开始");
        orderDao.updateOrderStatus(order.getUserId(),0);
        log.info("-----修改订单状态结束");

        log.info("下订单结束");

    }


}
